{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "31d19a8f",
   "metadata": {},
   "source": [
    "# 列表的方法\n",
    "\n",
    "* 学习方法  \n",
    "\n",
    "> 1.listnames.Tab键 查看所有方法名称\n",
    "\n",
    "> 2.help (listname.function) 查看其中一个方法的具体操作内容"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3f0a51be",
   "metadata": {},
   "source": [
    "* 1.append"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "aad9119b",
   "metadata": {},
   "outputs": [],
   "source": [
    "names = [\"Mike\",\"Mary\",\"Jan\",\"Jack\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "11ad4516",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function append:\n",
      "\n",
      "append(object, /) method of builtins.list instance\n",
      "    Append object to the end of the list.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.append)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "00cb2bc5",
   "metadata": {},
   "outputs": [],
   "source": [
    "names.append(\"huichun\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8e56ef5a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Mike', 'Mary', 'Jan', 'Jack', 'huichun']"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "00124ad8",
   "metadata": {},
   "source": [
    "* 2.insert"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "292656c4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function insert:\n",
      "\n",
      "insert(index, object, /) method of builtins.list instance\n",
      "    Insert object before index.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.insert)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "be422741",
   "metadata": {},
   "outputs": [],
   "source": [
    "names.insert(1,\"Alex\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "9c84209c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Mike', 'Alex', 'Mary', 'Jan', 'Jack', 'huichun']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3adb37ad",
   "metadata": {},
   "source": [
    "* 3.clear"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "e51728be",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function clear:\n",
      "\n",
      "clear() method of builtins.list instance\n",
      "    Remove all items from list.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.clear)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "55179596",
   "metadata": {},
   "outputs": [],
   "source": [
    "names = [\"Mike\",\"Mary\",\"Jan\",\"Jack\"]\n",
    "names.clear()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "4dee7fbd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5314b78c",
   "metadata": {},
   "source": [
    "* 4.copy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "50d70dcd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function copy:\n",
      "\n",
      "copy() method of builtins.list instance\n",
      "    Return a shallow copy of the list.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.copy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "220efbc0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Mike', 'Mary', 'Jan', 'Jack']"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names = [\"Mike\",\"Mary\",\"Jan\",\"Jack\"]\n",
    "names.copy()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a05fb8c",
   "metadata": {},
   "source": [
    "* 5.count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "fd7084c9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function count:\n",
      "\n",
      "count(value, /) method of builtins.list instance\n",
      "    Return number of occurrences of value.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "edc5f755",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names = [\"Mike\",\"Mary\",\"Jan\",\"Jack\"]\n",
    "names.count(\"Jan\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "25567b6b",
   "metadata": {},
   "source": [
    "* 6.extend"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "53f1eb57",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function extend:\n",
      "\n",
      "extend(iterable, /) method of builtins.list instance\n",
      "    Extend list by appending elements from the iterable.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.extend)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "f8d48169",
   "metadata": {},
   "outputs": [],
   "source": [
    "names = [\"Mike\",\"Mary\",\"Jan\",\"Jack\"]\n",
    "names.extend(\"Mike\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "f39cbd3f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Mike', 'Mary', 'Jan', 'Jack', 'M', 'i', 'k', 'e']"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53b0c8d1",
   "metadata": {},
   "source": [
    "* 7.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "98af25be",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function index:\n",
      "\n",
      "index(value, start=0, stop=9223372036854775807, /) method of builtins.list instance\n",
      "    Return first index of value.\n",
      "    \n",
      "    Raises ValueError if the value is not present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "933cebe9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names = [\"Mike\",\"Mary\",\"Jan\",\"Jack\"]\n",
    "names.index(\"Mary\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f90a43c",
   "metadata": {},
   "source": [
    "* 8.pop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "e8d37f3b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(index=-1, /) method of builtins.list instance\n",
      "    Remove and return item at index (default last).\n",
      "    \n",
      "    Raises IndexError if list is empty or index is out of range.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "feb77e3b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Mike'"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names = [\"Mike\",\"Mary\",\"Jan\",\"Jack\"]\n",
    "names.pop(0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2abee912",
   "metadata": {},
   "source": [
    "* 9.remove"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "c9b1019f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(value, /) method of builtins.list instance\n",
      "    Remove first occurrence of value.\n",
      "    \n",
      "    Raises ValueError if the value is not present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.remove)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "8d1ffbb1",
   "metadata": {},
   "outputs": [],
   "source": [
    "names = [\"Mike\",\"Mary\",\"Jan\",\"Jack\"]\n",
    "names.remove(\"Mary\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "526a0ba5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Mike', 'Jan', 'Jack']"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e0b8c9ca",
   "metadata": {},
   "source": [
    "* 10.reverse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "07792e31",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function reverse:\n",
      "\n",
      "reverse() method of builtins.list instance\n",
      "    Reverse *IN PLACE*.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.reverse)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "80ae3477",
   "metadata": {},
   "outputs": [],
   "source": [
    "names = [\"Mike\",\"Mary\",\"Jan\",\"Jack\"]\n",
    "names.reverse()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "40e800d3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Jan', 'Mary', 'Mike']"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a47673a2",
   "metadata": {},
   "source": [
    "* 11.sort"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "d89535b2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function sort:\n",
      "\n",
      "sort(*, key=None, reverse=False) method of builtins.list instance\n",
      "    Sort the list in ascending order and return None.\n",
      "    \n",
      "    The sort is in-place (i.e. the list itself is modified) and stable (i.e. the\n",
      "    order of two equal elements is maintained).\n",
      "    \n",
      "    If a key function is given, apply it once to each list item and sort them,\n",
      "    ascending or descending, according to their function values.\n",
      "    \n",
      "    The reverse flag can be set to sort in descending order.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.sort)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "99e92469",
   "metadata": {},
   "outputs": [],
   "source": [
    "names = [\"Mike\",\"Mary\",\"Jan\",\"Jack\"]\n",
    "names.sort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "60475615",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jack', 'Jan', 'Mary', 'Mike']"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7134f7e4",
   "metadata": {},
   "source": [
    "| Syntax    | Description |  example    |  remark     |\n",
    "| :-----------:|:-----------:  | :-----------: | :-----------:  |\n",
    "| append()  |增加元素，增加在列表的末尾|  详见1.append | 记得运行前面代码  |\n",
    "| insert()  |在索引前添加目标对象 | 详见2.insert     | 注意结果是插入在检索元素前面  |\n",
    "| clear()   | 清除列表中所有元素 | 详见3.clear      | 清除的是所有元素  |\n",
    "| copy()    | 复制列表中的元素  |  详见4.copy       | 自己运行出来的结果为什么没有double呢...  |\n",
    "| count()   | 检索元素在列表中出现的次数 | 详见5.count | 结果是出现次数,而不是列表元素的个数  |\n",
    "| extend()  |扩展列表中某个元素  |  详见6.extend     | 扩展的是某个具体元素  |\n",
    "| index()   | 检索该元素是否在列表中  | 详见7.index  | 在列表中则输出元素，不在则报错  |\n",
    "| pop()    | 如果元素不在列表中，则报错  | 详见8.pop  | 不在列表中则会报错   |\n",
    "| remove()  | 将某个元素从列表中移除  |  详见9.remove  | 移除列表中某个元素，不改变元素顺序  |\n",
    "| reserve()  | 将列表中的元素进行反转  | 详见10.reverse| 所有元素反向排序  |\n",
    "| sort()    | 将列表中的元素 进行排序 | 详见11.sort  |这个不是很懂捏...   |\n",
    "| Paragraph  | Text      |          |          |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7c46c991",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
